package com.controller;

import java.util.HashMap;

import java.util.List;
import java.util.Map;


import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dao.StudentDao;
import com.dao.TeacherDao;
import com.mybatis.po.Binding;
import com.mybatis.po.Student;
import com.mybatis.po.Teacher;


@Controller
public class LoginController {
	@Autowired
	public StudentDao studentDao ;
	@Autowired
	public TeacherDao teacherDao ;
	
	// 登录接口
	
	@RequestMapping("/login")
	@ResponseBody
	public Map<String, String> login(HttpServletRequest request) {
		String openid = request.getParameter("openid");
		List<Student> listStu = studentDao.selectStudentbyOpenid(openid);
		List<Teacher> listTea = teacherDao.selectTeacherbyOpenid(openid);
		Map<String,String> map = new HashMap<String, String>();
		if(listStu.size()!=0 ) {
			map.put("state", "1");
			map.put("message", "find");
			map.put("type","student");
			map.put("stuName", listStu.get(0).getSnoName());
			map.put("stuNo", listStu.get(0).getSno());
		}else if(listTea.size() != 0) {
			map.put("state", "1");
			map.put("message", "find");
			map.put("type","teather");
			map.put("teaName", listTea.get(0).getTname());
			map.put("teaNo", listTea.get(0).getDeptName());
		}else {
			map.put("state", "0");
			map.put("message", "no find");
		}
		return map;
	}
	
	// 绑定接口
	
	@RequestMapping("/binding")
	@ResponseBody
	public Map<String, String> register(Binding user) {
		System.out.print(user.getOpenid());
		Map<String,String> map = new HashMap<String, String>();
		// 因为还没有绑定，所以学生表没有openid信息，要通过account即工号或学号，去查是学生还是老师，返回学生或老师对象
		List<Student> listStu = studentDao.selectStudentbyAccount(user.getAccount());
		List<Teacher> listTea = teacherDao.selectTeacherbyAccount(user.getAccount());
		// 查找到是学生
		if (listStu.size()!= 0) {
			// 调用bindingStudent方法
			if(studentDao.bindingStudent(user)==1) {
				map.put("state", "bindding success");
				map.put("type","student");
				map.put("stuName", listStu.get(0).getSnoName());
				map.put("stuNo", listStu.get(0).getSno());
			}else {
				map.put("state", "bindding fail");
			}
		// 查找到是老师
		}else if(listTea.size() != 0) {
			// 调用bindingTeacher方法
			if(teacherDao.bindingTeacher(user) == 1) {
				map.put("state", "bindding success");
				map.put("type","teather");
				map.put("teaName", listTea.get(0).getTname());
			    map.put("teaNo", listTea.get(0).getTno());
			}else {
				map.put("state", "bindding fail");
			}
		// 数据库没有该用户的账号
		}else {
			map.put("state", "no find");
		}
		return map ;
	}	
	
	// 注销账户接口
	
	@RequestMapping("/logout")
	@ResponseBody
	public Map<String, String> logout(HttpServletRequest request){
		// 先获取openid
		String openid = request.getParameter("openid");
		// 通过openid去查是学生还是老师，返回学生或老师对象
		List<Student> listStu = studentDao.selectStudentbyOpenid(openid);
		List<Teacher> listTea = teacherDao.selectTeacherbyOpenid(openid);
		
		Map<String,String> map = new HashMap<String, String>();
		// 如果找到学生
		if(listStu.size() != 0 ) {
			// 获取该学生的学号
			String studentSno = listStu.get(0).getSno();
			// 进行注销，调用logoutStudent方法
			if(studentDao.logoutStudent(studentSno) == 1) {
				map.put("state", "logout success");
			}else {
				map.put("state", "logout fail");
			}
		// 如果找到老师
		}else if(listTea.size() != 0) {
			// 获取该老师的工号
			String teacherTno = listTea.get(0).getTno();
			// 进行注销，调用logoutTeacher方法
			if(teacherDao.logoutTeacher(teacherTno) == 1) {
				map.put("state", "logout success");
			}else {
				map.put("state", "logout fail");
			}
		// 如果用openid查不到，则代表没有绑定过，需要重新绑定
		}else {
			map.put("state", "no find");
		}
		return map;
	}
	
}
